package org.example.day09;

/**
 * 第三章节3.2.3: 执行join方法的线程出现异常
 *      线程A插队到线程B中，线程A执行过程中线程B等待，线程B等待中被interrupt时，会报InterruptedException
 */
//线程A
public class MyThread3 extends Thread{
    @Override
    public void run() {
        //长任务
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            Thread3B tb = new Thread3B();
            tb.start();
            Thread.sleep(5000); //线程A插队在线程B中
            Thread3C tc = new Thread3C(tb);
            tc.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

//线程B
class Thread3B extends Thread{
    @Override
    public void run() {
        try {
            MyThread3 m3 = new MyThread3();
            m3.start();
            //线程A插队,线程A是长任务
            m3.join();
            System.out.println("线程B run end");
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("线程B catch");
        }
    }
}

//线程C
class Thread3C extends Thread{
    private Thread3B tb;

    public Thread3C(Thread3B tb) {
        this.tb = tb;
    }

    @Override
    public void run() {
        tb.interrupt();
    }
}